/*
 *   Copyright (c) 2010-2022, SIUFUNG
 */
package net.siufung.market.base.permit.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.core.result.Result;
import net.siufung.core.result.ResultFail;
import net.siufung.core.result.ResultOk;
import net.siufung.market.base.permit.controller.assembler.RolePermitAssembler;
import net.siufung.market.base.permit.controller.model.dto.RolePermitDto;
import net.siufung.market.base.permit.controller.model.vo.RolePermitVo;
import net.siufung.market.base.permit.mapper.IRolePermitMapper;
import net.siufung.market.base.permit.mapper.entity.RolePermit;
import net.siufung.market.base.permit.service.IRoleService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统角色权限 前端控制器
 *
 * @author 陈建峰
 * @since 2022-07-05 16:14:50
 */
@Api(tags = "角色权限管理")
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/sys/role/permit")
public class RolePermitController {

    private final IRolePermitMapper rolePermitMapper;
    private final IRoleService roleService;


    /**
     * 获取 系统角色权限列表
     * @param roleId 属性
     * @return Result<List<RolePermitVo>>
     * */
    @ApiOperation(value = "获取系统角色权限列表")
    @GetMapping(value = "/list")
    public Result<List<RolePermitVo>> list(@RequestParam Integer roleId) {
        return Result.ok(
            RolePermitAssembler.fromList(rolePermitMapper.selectList(
                    new LambdaQueryWrapper<RolePermit>().eq(RolePermit::getRoleId, roleId))));
    }

    /**
     * 批量创建 系统角色权限
     * @param rolePermitDto 对象属性
     * @return 创建结果
     * */
    @ApiOperation(value = "批量创建 系统角色权限")
    @PostMapping(value="/createBatch")
    public Result<Void> createBatch(
            @RequestBody RolePermitDto rolePermitDto) {
        rolePermitDto.validated();
        if(roleService.allocationPermit(rolePermitDto.getRoleId(), rolePermitDto.toList())){
            return Result.ok(ResultOk.ADD);
        }
        return Result.fail(ResultFail.DATA_ADD_ERROR);
    }



}